//
//  UIBarButtonItem+CS.m
//
//  Created by 王城山 on 14-5-6.
//  Copyright (c) 2014年 itcast. All rights reserved.
//

#import "UIBarButtonItem+CS.h"

@implementation UIBarButtonItem (CS)
+ (UIBarButtonItem *)itemWithIcon:(NSString *)icon highIcon:(NSString *)highIcon title:(NSString *)title target:(id)target action:(SEL)action
{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    if (icon&&highIcon) {
        [button setBackgroundImage:[UIImage imageNamed:icon] forState:UIControlStateNormal];
        [button setBackgroundImage:[UIImage imageNamed:highIcon] forState:UIControlStateHighlighted];
        button.frame = (CGRect){CGPointZero, button.currentBackgroundImage.size};
    }
    [button setTitle:title forState:UIControlStateNormal];
    button.titleLabel.font = MidFont;
    CGSize size = [title boundingRectWithSize:CGSizeMake(MAXFLOAT, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:button.titleLabel.font} context:nil].size;
    button.frame = (CGRect){CGPointZero,size};
    [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    return [[UIBarButtonItem alloc] initWithCustomView:button];
}

- (void)setTitleColor:(UIColor *)titleColor
{
    if (self.customView) {
        UIButton *button = (UIButton *)self.customView;
        [button setTitleColor:titleColor forState:UIControlStateNormal];
    }
}

- (UIColor *)titleColor
{
    if (self.customView) {
        UIButton *button = (UIButton *)self.customView;
        return button.titleLabel.textColor;
    }
    else{
        return nil;
    }
}
@end
